package com.example.springboot.controller;

import com.example.springboot.entity.Student;
import com.example.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 获取所有学生
    @GetMapping("")
    public List<Student> getAllStudent() {
        return studentService.getAllStudent();
    }

    // 获取所有邮箱
    @GetMapping("/email")
    public List<Student> getAllEmail() {
        return studentService.getAllEmail();
    }

    @GetMapping("/info")
    public ResponseEntity<Student> getStudentInfo(@RequestParam String email) {
        Student student = studentService.getUserInfo(email);
        if (student != null) {
            return ResponseEntity.ok(student);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 登录
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody Student loginRequest) {
        System.out.println("尝试登录的邮箱或手机号: " + loginRequest.getEmail());
        Student student = studentService.findStudent(loginRequest);
        if (student != null) {
            return ResponseEntity.ok(student.toString());
        } else {
            return ResponseEntity.status(401).body("登录失败");
        }
    }

    // 注册
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody Student student) {
        try {
            studentService.register(student);
            return ResponseEntity.ok("注册成功");
        } catch (Exception e) {
            return ResponseEntity.status(400).body(e.getMessage());
        }
    }

    @GetMapping("/check-email/{email}")
    public ResponseEntity<Map<String, Object>> checkEmailExists(@PathVariable String email) {
        boolean exists = studentService.emailExists(email);
        Map<String, Object> response = new HashMap<>();
        response.put("exists", exists);
        return ResponseEntity.ok(response);
    }

    // 更新邮箱
    @PostMapping("/update-email")
    public ResponseEntity<String> updateEmail(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String currentPassword = request.get("currentPassword");

        // 验证当前密码是否正确
        if (!studentService.checkPassword(email, currentPassword)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("当前密码错误");
        }

        // 检查邮箱是否已被注册
        if (studentService.emailExists(email)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("邮箱已被注册");
        }

        // 更新邮箱
        studentService.updateEmail(email, email);
        return ResponseEntity.ok("邮箱更新成功");
    }

    // 更新手机号
    @PostMapping("/update-phone")
    public ResponseEntity<String> updatePhone(@RequestBody Map<String, String> request) {
        String phoneNumber = request.get("phoneNumber");
        String currentPassword = request.get("currentPassword");

        // 验证当前密码是否正确
        if (!studentService.checkPassword(phoneNumber, currentPassword)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("当前密码错误");
        }

        // 检查手机号是否已被注册
        if (studentService.phoneExists(phoneNumber)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("手机号已被注册");
        }

        // 更新手机号
        studentService.updatePhone(phoneNumber, phoneNumber);
        return ResponseEntity.ok("手机号更新成功");
    }

    // 更新密码
    @PostMapping("/update-password")
    public ResponseEntity<String> updatePassword(@RequestBody Map<String, String> request) {
        String currentPassword = request.get("currentPassword");
        String newPassword = request.get("newPassword");

        // 验证当前密码是否正确
        if (!studentService.checkPassword(request.get("email"), currentPassword)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("当前密码错误");
        }

        // 更新密码
        studentService.updatePassword(request.get("email"), newPassword);
        return ResponseEntity.ok("密码更新成功");
    }
}
